package com.lanou.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapplication.entity.Img;
import com.lanou.myapplication.entity.ToplineDetail;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/29.
 */
public class ToplineDetailActivity extends Activity {
    private TextView title, source, ptime;
    private WebView body;
    private ImageView current_image;

    private RequestQueue queue;

    private String path;

    private WebView Body;
    private String Title;
    private String Source;
    private String Ptime;
    private ScrollView scrollView;
    private String  docid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toplinedetail);
        scrollView =(ScrollView)findViewById(R.id.scrollView1);
        title = (TextView) findViewById(R.id.title);
        source = (TextView) findViewById(R.id.source);
        ptime = (TextView) findViewById(R.id.ptime);
        body = (WebView) findViewById(R.id.body);

        current_image = (ImageView) findViewById(R.id.current_image);
        queue = Volley.newRequestQueue(this);
        getData();
        requestBitmap();

    }

    public void getData() {
        docid = getIntent().getStringExtra("docid");



    }

    private void requestBitmap() {
        Intent intent = getIntent();
        String url = "http://c.m.163.com/nc/article/"+docid +"/full.html";

        JsonObjectRequest request = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {
                    List<ToplineDetail> list = new ArrayList<>();
                   List<Img> imgs = new ArrayList<>();
                    //String body = new String();

                    Gson gson = new Gson();

                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has(docid)) {
                            try {
                                JSONObject object = response.getJSONObject(docid);
                                if (object != null && object.has("img")) {
                                    JSONArray array = object.getJSONArray("img");
                                    for (int i = 0; i < array.length(); i++) {
                                        String str = array.getString(i);
                                        Img img = gson.fromJson(str, Img.class);
                                        imgs.add(img);
                                        path = imgs.get(0).getSrc();
                                        getImage(path, current_image);
                                    }
                                }
                                if (object != null && object.has("body")) {

                                    String str = object.toString();
                                 //   Log.i("jjdjgk", object + "");
                                    ToplineDetail detail = gson.fromJson(str, ToplineDetail.class);
                                    list.add(detail);
                                   // Body = list.get(0).getBody();
                                  //  body.setText(Body);
                                    final String mimrTypeString = "text/html";
                                    final String encoding = "utf-8";
                                    final String htmlString = list.get(0).getBody();
                                    body.loadDataWithBaseURL("fake://not/needed",
                                            htmlString, mimrTypeString, encoding, "");
                                    body.setInitialScale(120);



                              //      Log.i("hjdhfh", Body + "");
                                }
                                if (object != null && object.has("title")) {

                                    String str = object.toString();
                                    ToplineDetail detail = gson.fromJson(str, ToplineDetail.class);
                                    list.add(detail);
                                    Title = list.get(0).getTitle();
                                    title.setText(Title);

                                }if (object != null && object.has("source")) {

                                    String str = object.toString();
                                    ToplineDetail detail = gson.fromJson(str, ToplineDetail.class);
                                    list.add(detail);
                                    Source = list.get(0).getSource();
                                    source.setText(Source);

                                }if (object != null && object.has("ptime")) {

                                    String str = object.toString();
                                    ToplineDetail detail = gson.fromJson(str, ToplineDetail.class);
                                    list.add(detail);
                                    Ptime = list.get(0).getPtime();
                                    ptime.setText(Ptime);

                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();

                    }
                }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;


            }

//            protected Response<String> parseNetworkResponse(NetworkResponse response) {
//                try {
//                    String jsonString = new String(response.data, "UTF-8");
//                    return Response.success(jsonString,
//                            HttpHeaderParser.parseCacheHeaders(response));
//                } catch (UnsupportedEncodingException e) {
//                    return Response.error(new ParseError(e));
//                } catch (Exception je) {
//                    return Response.error(new ParseError(je));
//                }
        };

        queue.add(request);
    }

    /**
     * 将上面解析出的图片地址进行解析成图片
     *
     * @param path
     * @param imageView
     */
    public void getImage(String path, final ImageView imageView) {
        //path = imgs.get(0).getSrc();
        ImageRequest request = new ImageRequest(path, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 300, 300, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }
        );
        queue.add(request);
    }
}
